package collection.set;

import java.util.Comparator;
import java.util.Iterator;
import java.util.TreeSet;

class StrComparator<T> implements Comparator<T> {

	@Override
	public int compare(T o1, T o2) {
		return o1.toString().toLowerCase().compareTo(o2.toString().toLowerCase());
	}
	
}

public class TreeSetExample {
	TreeSet<String> tree;
	
	public TreeSetExample() {
		tree = new TreeSet<>(new StrComparator());
		addData();
	}

	private void addData() {
		tree.add("hello");
		tree.add("bonjour");
		tree.add("bonjour");
		tree.add("Xin chao");
		tree.add("xin Chao");		
	}
	
	public void display(){
		Iterator<String> iter = tree.iterator();
		while (iter.hasNext()){
			System.out.println(iter.next());
		}
		
		System.out.println();
		System.out.println("hset union hset: ");
		addSet(tree);
		
		iter = tree.iterator();
		while (iter.hasNext()){
			System.out.println(iter.next());
		}
	}
	
	public void addSet(TreeSet<String> set){
		tree.addAll(set);
	}
	
	public static void main(String[] args) {
		new TreeSetExample().display();
	}
}
